package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ZslBanner;
import com.cz.zslu.entity.ZslFollow;
import com.cz.zslu.entity.ZslNews;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.IZslFollowService;
import com.cz.zslu.service.IZslNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 资讯头条 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/sys/news")
@Api(description = "资讯头条管理")
public class NewsController extends BaseController<IZslNewsService, ZslNews> {


    @ApiOperation("获取全部资讯头条分页")
    @IgnoreAuth
    @RequestMapping(value = "/getAllNewPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslNews>> getAllNewPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                     @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslNews> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        Page<ZslNews> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<ZslNews> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "新增成功", baseService.selectById(id));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/saveNewInfo", method = RequestMethod.POST)
    public ReturnObject<ZslNews> saveNewInfo(@ApiParam("主题") @RequestParam String title,
                                             @ApiParam("内容") @RequestParam String content) {

        ZslNews info = new ZslNews();
        info.setTitle(title);
        info.setContent(content);
        baseService.insert(info);

        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delNewById", method = RequestMethod.POST)
    public ReturnObject<ZslNews> delNewById(@ApiParam("id") @RequestParam Long id) {

        ZslNews info = baseService.selectById(id);
        info.setState(1);

        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }

}

